package cn.edu.ujn.booksshop.controller;

import java.util.List;

import javax.servlet.http.HttpSession;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.booksshop.dao.Author;
import cn.edu.ujn.booksshop.dao.User;
import cn.edu.ujn.booksshop.service.IAuthorService;
import cn.edu.ujn.util.Page;

@Controller
public class AuthorController {

	@Autowired
	private IAuthorService authorService;
	@GetMapping("/authors/list.action")
	public String list(Model model,@RequestParam(defaultValue="1")Integer page , @RequestParam(defaultValue ="10")Integer rows,String authorName )
	{
		Page<Author> selectAll = this.authorService.selectAll(page, rows,authorName);
		model.addAttribute("page", selectAll);
//		System.out.println(authorName);
		return "authors" ;
	}
	
	@RequestMapping("/author/create.action")
	@ResponseBody
	public String authorCreat(Author author,HttpSession session ) {
		User user = (User) session.getAttribute("USER_SESSION");
		author.setIsdeleted(false);
//		System.out.println(author.getName());
//		System.out.println(author.getBiography());
//		System.out.println(author.getIsdeleted());
	int rows=authorService.insert(author);
	if(rows > 0){
        return "OK";
    }else{
        return "FAIL";
    }	
	}
	
	@RequestMapping("/author/getAuthorById.action")
	@ResponseBody
	public Author getAuthorById(Integer id) {
		Author author = authorService.selectByPrimaryKey(id);
//		System.out.println(author.getAuthorid());
//		System.out.println(author.getBiography());
	    return author;
	}

	@RequestMapping("/author/update.action")
	@ResponseBody
	public String authorUpdate(Author author) {
//		System.out.println(author.getBiography());
//		System.out.println(author.getName());
//		System.out.println(author.getAuthorid());
	    int rows = authorService.updateByPrimaryKey(author);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
		
	}
	
	@RequestMapping("/author/delete.action")
	@ResponseBody
	public String authorDelete(Integer id) {
	    int rows = authorService.deleteByPrimaryKey(id);
	    if(rows > 0){			
	        return "OK";
	    }else{
	        return "FAIL";			
	    }
	}


}
